<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Media | Auto.js 4.1.0 文档</title>
  <link rel="stylesheet" href="assets/fonts.css">
  <link rel="stylesheet" href="assets/style.css">
  <link rel="stylesheet" href="assets/sh.css">
  <link rel="canonical" href="https://nodejs.org/api/media.html">
</head>
<body class="alt apidoc" id="api-section-media">
  <div id="content" class="clearfix">
    <div id="column2" class="interior">
      <div id="intro" class="interior">
        <a href="/" title="返回首页">
          Auto.js
        </a>
      </div>
      <ul>
<li><a class="nav-documentation" href="documentation.html">关于本文档</a></li>
<li><a class="nav-http-www-w3school-com-cn-js-pro_js_syntax-asp" href="http://www.w3school.com.cn/js/pro_js_syntax.asp">W3CSchool - ECMAScript教程</a></li>
<li><a class="nav-http-es6-ruanyifeng-com-README" href="http://es6.ruanyifeng.com/#README">阮一峰 - ECMAScript 6教程</a></li>
</ul>
<div class="line"></div>

<ul>
<li><a class="nav-overview" href="overview.html">Overview - 综述</a></li>
<li><a class="nav-qa" href="qa.html">Q&amp;A - 常见问题</a></li>
<li><a class="nav-app" href="app.html">App - 应用</a></li>
<li><a class="nav-console" href="console.html">Console - 控制台</a></li>
<li><a class="nav-coordinates-based-automation" href="coordinates-based-automation.html">CoordinatesBasedAutomation - 基于坐标的操作</a></li>
<li><a class="nav-device" href="device.html">Device - 设备</a></li>
<li><a class="nav-dialogs" href="dialogs.html">Dialogs - 对话框</a></li>
<li><a class="nav-engines" href="engines.html">Engines - 脚本引擎</a></li>
<li><a class="nav-events" href="events.html">Events - 事件与监听</a></li>
<li><a class="nav-floaty" href="floaty.html">Floaty - 悬浮窗</a></li>
<li><a class="nav-files" href="files.html">Files - 文件系统</a></li>
<li><a class="nav-globals" href="globals.html">Globals - 一般全局函数</a></li>
<li><a class="nav-http" href="http.html">Http - HTTP</a></li>
<li><a class="nav-images" href="images.html">Images - 图片与图色处理</a></li>
<li><a class="nav-keys" href="keys.html">Keys - 按键模拟</a></li>
<li><a class="nav-media active" href="media.html">Media - 多媒体</a></li>
<li><a class="nav-modules" href="modules.html">Modules - 模块</a></li>
<li><a class="nav-widgets-based-automation" href="widgets-based-automation.html">WidgetsBasedAutomation - 基于控件的操作</a></li>
<li><a class="nav-sensors" href="sensors.html">Sensors - 传感器</a></li>
<li><a class="nav-shell" href="shell.html">Shell - Shell命令</a></li>
<li><a class="nav-storages" href="storages.html">Storages - 本地存储</a></li>
<li><a class="nav-threads" href="threads.html">Threads - 多线程</a></li>
<li><a class="nav-timers" href="timers.html">Timers - 定时器</a></li>
<li><a class="nav-ui" href="ui.html">UI - 用户界面</a></li>
<li><a class="nav-https-developer-mozilla-org-zh-CN-docs-Mozilla-Projects-Rhino-Scripting_Java" href="https://developer.mozilla.org/zh-CN/docs/Mozilla/Projects/Rhino/Scripting_Java">Work with Java - 调用Java API</a></li>
</ul>
<div class="line"></div>

<ul>
<li><a class="nav-https-github-com-hyb1996-NoRootScriptDroid" href="https://github.com/hyb1996/NoRootScriptDroid">GitHub项目 &amp; Issue提交</a></li>
<li><a class="nav-http-autojs-org" href="http://autojs.org">Auto.js交流社区</a></li>
</ul>

    </div>

    <div id="column1" data-id="media" class="interior">
      <header>
        <h1>Auto.js 4.1.0 文档</h1>
        <div id="gtoc">
          <p>
            <a href="index.html" name="toc">索引</a> |
            <a href="all.html">查看全部</a>
          </p>
        </div>
        <hr>
      </header>

      <div id="toc">
        <h2>目录</h2>
        <ul>
<li><span class="stability_2"><a href="#media_media">Media</a></span><ul>
<li><span class="stability_undefined"><a href="#media_media_scanfile_path">media.scanFile(path)</a></span></li>
<li><span class="stability_undefined"><a href="#media_media_playmusic_path_volume_looping">media.playMusic(path[, volume, looping])</a></span></li>
<li><span class="stability_undefined"><a href="#media_media_musicseekto_msec">media.musicSeekTo(msec)</a></span></li>
<li><span class="stability_undefined"><a href="#media_media_pausemusic">media.pauseMusic()</a></span></li>
<li><span class="stability_undefined"><a href="#media_media_resumemusic">media.resumeMusic()</a></span></li>
<li><span class="stability_undefined"><a href="#media_media_stopmusic">media.stopMusic()</a></span></li>
<li><span class="stability_undefined"><a href="#media_media_ismusicplaying">media.isMusicPlaying()</a></span></li>
<li><span class="stability_undefined"><a href="#media_media_getmusicduration">media.getMusicDuration()</a></span></li>
<li><span class="stability_undefined"><a href="#media_media_getmusiccurrentposition">media.getMusicCurrentPosition()</a></span></li>
</ul>
</li>
</ul>

      </div>

      <div id="apicontent">
        <h1>Media<span><a class="mark" href="#media_media" id="media_media">#</a></span></h1>
<div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>media模块提供多媒体编程的支持。目前仅支持音乐播放和媒体文件扫描。后续会结合UI加入视频播放等功能。</p>
<p>需要注意是，使用该模块播放音乐时是在后台异步播放的，在脚本结束后会自动结束播放，因此可能需要插入诸如<code>sleep()</code>的语句来使脚本保持运行。例如：</p>
<pre><code>//播放音乐
media.playMusic(&quot;/sdcard/1.mp3&quot;);
//让音乐播放完
sleep(media.getMusicDuration());
</code></pre><h2>media.scanFile(path)<span><a class="mark" href="#media_media_scanfile_path" id="media_media_scanfile_path">#</a></span></h2>
<div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 媒体文件路径</li>
</ul>
</div><p>扫描路径path的媒体文件，将它加入媒体库中；或者如果该文件以及被删除，则通知媒体库移除该文件。</p>
<p>媒体库包括相册、音乐库等，因此该函数可以用于把某个图片文件加入相册。</p>
<pre><code>//请求截图
requestScreenCapture(false);
//截图
var im = captureScreen();
var path = &quot;/sdcard/screenshot.png&quot;;
//保存图片
im.saveTo(path);
//把图片加入相册
media.scanFile(path);
</code></pre><h2>media.playMusic(path[, volume, looping])<span><a class="mark" href="#media_media_playmusic_path_volume_looping" id="media_media_playmusic_path_volume_looping">#</a></span></h2>
<div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 音乐文件路径</li>
<li><code>volume</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 播放音量，为0~1的浮点数，默认为1</li>
<li><code>looping</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a> 是否循环播放，如果looping为<code>true</code>则循环播放，默认为<code>false</code></li>
</ul>
</div><p>播放音乐文件path。该函数不会显示任何音乐播放界面。如果文件不存在或者文件不是受支持的音乐格式，则抛出<code>UncheckedIOException</code>异常。</p>
<pre><code>//播放音乐
media.playMusic(&quot;/sdcard/1.mp3&quot;);
//让音乐播放完
sleep(media.getMusicDuration());
</code></pre><p>如果要循环播放音乐，则使用looping参数：</p>
<pre><code>
</code></pre><p>//传递第三个参数为true以循环播放音乐
media.playMusic(&quot;/sdcard/1.mp3&quot;, 1, true);
//等待三次播放的时间
sleep(media.getMusicDuration() * 3);</p>
<pre><code>
</code></pre><p>如果要使用音乐播放器播放音乐，调用<code>app.viewFile(path)</code>函数。</p>
<h2>media.musicSeekTo(msec)<span><a class="mark" href="#media_media_musicseekto_msec" id="media_media_musicseekto_msec">#</a></span></h2>
<div class="signature"><ul>
<li><code>msec</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 毫秒数，表示音乐进度</li>
</ul>
</div><p>把当前播放进度调整到时间msec的位置。如果当前没有在播放音乐，则调用函数没有任何效果。</p>
<p>例如，要把音乐调到1分钟的位置，为<code>media.musicSeekTo(60 * 1000)</code>。</p>
<pre><code>//播放音乐
media.playMusic(&quot;/sdcard/1.mp3&quot;);
//调整到30秒的位置
media.musicSeekTo(30 * 1000);
//等待音乐播放完成
sleep(media.getMusicDuration() - 30 * 1000);
</code></pre><h2>media.pauseMusic()<span><a class="mark" href="#media_media_pausemusic" id="media_media_pausemusic">#</a></span></h2>
<p>暂停音乐播放。如果当前没有在播放音乐，则调用函数没有任何效果。</p>
<h2>media.resumeMusic()<span><a class="mark" href="#media_media_resumemusic" id="media_media_resumemusic">#</a></span></h2>
<p>继续音乐播放。如果当前没有播放过音乐，则调用该函数没有任何效果。</p>
<h2>media.stopMusic()<span><a class="mark" href="#media_media_stopmusic" id="media_media_stopmusic">#</a></span></h2>
<p>停止音乐播放。如果当前没有在播放音乐，则调用函数没有任何效果。</p>
<h2>media.isMusicPlaying()<span><a class="mark" href="#media_media_ismusicplaying" id="media_media_ismusicplaying">#</a></span></h2>
<div class="signature"><ul>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a></li>
</ul>
</div><p>返回当前是否正在播放音乐。</p>
<h2>media.getMusicDuration()<span><a class="mark" href="#media_media_getmusicduration" id="media_media_getmusicduration">#</a></span></h2>
<div class="signature"><ul>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li>
</ul>
</div><p>返回当前音乐的时长。单位毫秒。</p>
<h2>media.getMusicCurrentPosition()<span><a class="mark" href="#media_media_getmusiccurrentposition" id="media_media_getmusiccurrentposition">#</a></span></h2>
<div class="signature"><ul>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li>
</ul>
</div><p>返回当前音乐的播放进度(已经播放的时间)，单位毫秒。</p>

      </div>
    </div>
  </div>
  <script src="assets/sh_main.js"></script>
  <script src="assets/sh_javascript.min.js"></script>
  <script>highlight(undefined, undefined, 'pre');</script>
  <!-- __TRACKING__ -->
</body>
</html>